root / hw / qdev-properties.c @ 2b7a050a
History | View | Annotate | Download (10.1 kB)
1 | 14b41872 | Gerd Hoffmann | #include "sysemu.h" |
---|---|---|---|
2 | ee6847d1 | Gerd Hoffmann | #include "qdev.h" |
3 | ee6847d1 | Gerd Hoffmann | |
4 | ee6847d1 | Gerd Hoffmann | void *qdev_get_prop_ptr(DeviceState *dev, Property *prop)
|
5 | ee6847d1 | Gerd Hoffmann | { |
6 | ee6847d1 | Gerd Hoffmann | void *ptr = dev;
|
7 | ee6847d1 | Gerd Hoffmann | ptr += prop->offset; |
8 | ee6847d1 | Gerd Hoffmann | return ptr;
|
9 | ee6847d1 | Gerd Hoffmann | } |
10 | ee6847d1 | Gerd Hoffmann | |
11 | ee6847d1 | Gerd Hoffmann | /* --- 16bit integer --- */
|
12 | ee6847d1 | Gerd Hoffmann | |
13 | ee6847d1 | Gerd Hoffmann | static int parse_uint16(DeviceState *dev, Property *prop, const char *str) |
14 | ee6847d1 | Gerd Hoffmann | { |
15 | ee6847d1 | Gerd Hoffmann | uint16_t *ptr = qdev_get_prop_ptr(dev, prop); |
16 | ee6847d1 | Gerd Hoffmann | const char *fmt; |
17 | ee6847d1 | Gerd Hoffmann | |
18 | ee6847d1 | Gerd Hoffmann | /* accept both hex and decimal */
|
19 | ee6847d1 | Gerd Hoffmann | fmt = strncasecmp(str, "0x",2) == 0 ? "%" PRIx16 : "%" PRIu16; |
20 | ee6847d1 | Gerd Hoffmann | if (sscanf(str, fmt, ptr) != 1) |
21 | ee6847d1 | Gerd Hoffmann | return -1; |
22 | ee6847d1 | Gerd Hoffmann | return 0; |
23 | ee6847d1 | Gerd Hoffmann | } |
24 | ee6847d1 | Gerd Hoffmann | |
25 | ee6847d1 | Gerd Hoffmann | static int print_uint16(DeviceState *dev, Property *prop, char *dest, size_t len) |
26 | ee6847d1 | Gerd Hoffmann | { |
27 | ee6847d1 | Gerd Hoffmann | uint16_t *ptr = qdev_get_prop_ptr(dev, prop); |
28 | ee6847d1 | Gerd Hoffmann | return snprintf(dest, len, "%" PRIu16, *ptr); |
29 | ee6847d1 | Gerd Hoffmann | } |
30 | ee6847d1 | Gerd Hoffmann | |
31 | ee6847d1 | Gerd Hoffmann | PropertyInfo qdev_prop_uint16 = { |
32 | ee6847d1 | Gerd Hoffmann | .name = "uint16",
|
33 | ee6847d1 | Gerd Hoffmann | .type = PROP_TYPE_UINT16, |
34 | ee6847d1 | Gerd Hoffmann | .size = sizeof(uint16_t),
|
35 | ee6847d1 | Gerd Hoffmann | .parse = parse_uint16, |
36 | ee6847d1 | Gerd Hoffmann | .print = print_uint16, |
37 | ee6847d1 | Gerd Hoffmann | }; |
38 | ee6847d1 | Gerd Hoffmann | |
39 | ee6847d1 | Gerd Hoffmann | /* --- 32bit integer --- */
|
40 | ee6847d1 | Gerd Hoffmann | |
41 | ee6847d1 | Gerd Hoffmann | static int parse_uint32(DeviceState *dev, Property *prop, const char *str) |
42 | ee6847d1 | Gerd Hoffmann | { |
43 | ee6847d1 | Gerd Hoffmann | uint32_t *ptr = qdev_get_prop_ptr(dev, prop); |
44 | ee6847d1 | Gerd Hoffmann | const char *fmt; |
45 | ee6847d1 | Gerd Hoffmann | |
46 | ee6847d1 | Gerd Hoffmann | /* accept both hex and decimal */
|
47 | ee6847d1 | Gerd Hoffmann | fmt = strncasecmp(str, "0x",2) == 0 ? "%" PRIx32 : "%" PRIu32; |
48 | ee6847d1 | Gerd Hoffmann | if (sscanf(str, fmt, ptr) != 1) |
49 | ee6847d1 | Gerd Hoffmann | return -1; |
50 | ee6847d1 | Gerd Hoffmann | return 0; |
51 | ee6847d1 | Gerd Hoffmann | } |
52 | ee6847d1 | Gerd Hoffmann | |
53 | ee6847d1 | Gerd Hoffmann | static int print_uint32(DeviceState *dev, Property *prop, char *dest, size_t len) |
54 | ee6847d1 | Gerd Hoffmann | { |
55 | ee6847d1 | Gerd Hoffmann | uint32_t *ptr = qdev_get_prop_ptr(dev, prop); |
56 | ee6847d1 | Gerd Hoffmann | return snprintf(dest, len, "%" PRIu32, *ptr); |
57 | ee6847d1 | Gerd Hoffmann | } |
58 | ee6847d1 | Gerd Hoffmann | |
59 | ee6847d1 | Gerd Hoffmann | PropertyInfo qdev_prop_uint32 = { |
60 | ee6847d1 | Gerd Hoffmann | .name = "uint32",
|
61 | ee6847d1 | Gerd Hoffmann | .type = PROP_TYPE_UINT32, |
62 | ee6847d1 | Gerd Hoffmann | .size = sizeof(uint32_t),
|
63 | ee6847d1 | Gerd Hoffmann | .parse = parse_uint32, |
64 | ee6847d1 | Gerd Hoffmann | .print = print_uint32, |
65 | ee6847d1 | Gerd Hoffmann | }; |
66 | ee6847d1 | Gerd Hoffmann | |
67 | ee6847d1 | Gerd Hoffmann | /* --- 32bit hex value --- */
|
68 | ee6847d1 | Gerd Hoffmann | |
69 | ee6847d1 | Gerd Hoffmann | static int parse_hex32(DeviceState *dev, Property *prop, const char *str) |
70 | ee6847d1 | Gerd Hoffmann | { |
71 | ee6847d1 | Gerd Hoffmann | uint32_t *ptr = qdev_get_prop_ptr(dev, prop); |
72 | ee6847d1 | Gerd Hoffmann | |
73 | ee6847d1 | Gerd Hoffmann | if (sscanf(str, "%" PRIx32, ptr) != 1) |
74 | ee6847d1 | Gerd Hoffmann | return -1; |
75 | ee6847d1 | Gerd Hoffmann | return 0; |
76 | ee6847d1 | Gerd Hoffmann | } |
77 | ee6847d1 | Gerd Hoffmann | |
78 | ee6847d1 | Gerd Hoffmann | static int print_hex32(DeviceState *dev, Property *prop, char *dest, size_t len) |
79 | ee6847d1 | Gerd Hoffmann | { |
80 | ee6847d1 | Gerd Hoffmann | uint32_t *ptr = qdev_get_prop_ptr(dev, prop); |
81 | ee6847d1 | Gerd Hoffmann | return snprintf(dest, len, "0x%" PRIx32, *ptr); |
82 | ee6847d1 | Gerd Hoffmann | } |
83 | ee6847d1 | Gerd Hoffmann | |
84 | ee6847d1 | Gerd Hoffmann | PropertyInfo qdev_prop_hex32 = { |
85 | ee6847d1 | Gerd Hoffmann | .name = "hex32",
|
86 | ee6847d1 | Gerd Hoffmann | .type = PROP_TYPE_UINT32, |
87 | ee6847d1 | Gerd Hoffmann | .size = sizeof(uint32_t),
|
88 | ee6847d1 | Gerd Hoffmann | .parse = parse_hex32, |
89 | ee6847d1 | Gerd Hoffmann | .print = print_hex32, |
90 | ee6847d1 | Gerd Hoffmann | }; |
91 | ee6847d1 | Gerd Hoffmann | |
92 | 5a053d1f | Blue Swirl | /* --- 64bit integer --- */
|
93 | 5a053d1f | Blue Swirl | |
94 | 5a053d1f | Blue Swirl | static int parse_uint64(DeviceState *dev, Property *prop, const char *str) |
95 | 5a053d1f | Blue Swirl | { |
96 | 5a053d1f | Blue Swirl | uint64_t *ptr = qdev_get_prop_ptr(dev, prop); |
97 | 5a053d1f | Blue Swirl | const char *fmt; |
98 | 5a053d1f | Blue Swirl | |
99 | 5a053d1f | Blue Swirl | /* accept both hex and decimal */
|
100 | 5a053d1f | Blue Swirl | fmt = strncasecmp(str, "0x",2) == 0 ? "%" PRIx64 : "%" PRIu64; |
101 | 5a053d1f | Blue Swirl | if (sscanf(str, fmt, ptr) != 1) |
102 | 5a053d1f | Blue Swirl | return -1; |
103 | 5a053d1f | Blue Swirl | return 0; |
104 | 5a053d1f | Blue Swirl | } |
105 | 5a053d1f | Blue Swirl | |
106 | 5a053d1f | Blue Swirl | static int print_uint64(DeviceState *dev, Property *prop, char *dest, size_t len) |
107 | 5a053d1f | Blue Swirl | { |
108 | 5a053d1f | Blue Swirl | uint64_t *ptr = qdev_get_prop_ptr(dev, prop); |
109 | 5a053d1f | Blue Swirl | return snprintf(dest, len, "%" PRIu64, *ptr); |
110 | 5a053d1f | Blue Swirl | } |
111 | 5a053d1f | Blue Swirl | |
112 | 5a053d1f | Blue Swirl | PropertyInfo qdev_prop_uint64 = { |
113 | 5a053d1f | Blue Swirl | .name = "uint64",
|
114 | 5a053d1f | Blue Swirl | .type = PROP_TYPE_UINT64, |
115 | 5a053d1f | Blue Swirl | .size = sizeof(uint64_t),
|
116 | 5a053d1f | Blue Swirl | .parse = parse_uint64, |
117 | 5a053d1f | Blue Swirl | .print = print_uint64, |
118 | 5a053d1f | Blue Swirl | }; |
119 | 5a053d1f | Blue Swirl | |
120 | 5a053d1f | Blue Swirl | /* --- 64bit hex value --- */
|
121 | 5a053d1f | Blue Swirl | |
122 | 5a053d1f | Blue Swirl | static int parse_hex64(DeviceState *dev, Property *prop, const char *str) |
123 | 5a053d1f | Blue Swirl | { |
124 | 5a053d1f | Blue Swirl | uint64_t *ptr = qdev_get_prop_ptr(dev, prop); |
125 | 5a053d1f | Blue Swirl | |
126 | 5a053d1f | Blue Swirl | if (sscanf(str, "%" PRIx64, ptr) != 1) |
127 | 5a053d1f | Blue Swirl | return -1; |
128 | 5a053d1f | Blue Swirl | return 0; |
129 | 5a053d1f | Blue Swirl | } |
130 | 5a053d1f | Blue Swirl | |
131 | 5a053d1f | Blue Swirl | static int print_hex64(DeviceState *dev, Property *prop, char *dest, size_t len) |
132 | 5a053d1f | Blue Swirl | { |
133 | 5a053d1f | Blue Swirl | uint64_t *ptr = qdev_get_prop_ptr(dev, prop); |
134 | 5a053d1f | Blue Swirl | return snprintf(dest, len, "0x%" PRIx64, *ptr); |
135 | 5a053d1f | Blue Swirl | } |
136 | 5a053d1f | Blue Swirl | |
137 | 5a053d1f | Blue Swirl | PropertyInfo qdev_prop_hex64 = { |
138 | 5a053d1f | Blue Swirl | .name = "hex64",
|
139 | 5a053d1f | Blue Swirl | .type = PROP_TYPE_UINT64, |
140 | 5a053d1f | Blue Swirl | .size = sizeof(uint64_t),
|
141 | 5a053d1f | Blue Swirl | .parse = parse_hex64, |
142 | 5a053d1f | Blue Swirl | .print = print_hex64, |
143 | 5a053d1f | Blue Swirl | }; |
144 | 5a053d1f | Blue Swirl | |
145 | 14b41872 | Gerd Hoffmann | /* --- drive --- */
|
146 | 14b41872 | Gerd Hoffmann | |
147 | 14b41872 | Gerd Hoffmann | static int parse_drive(DeviceState *dev, Property *prop, const char *str) |
148 | 14b41872 | Gerd Hoffmann | { |
149 | 14b41872 | Gerd Hoffmann | DriveInfo **ptr = qdev_get_prop_ptr(dev, prop); |
150 | 14b41872 | Gerd Hoffmann | |
151 | 14b41872 | Gerd Hoffmann | *ptr = drive_get_by_id(str); |
152 | 14b41872 | Gerd Hoffmann | if (*ptr == NULL) |
153 | 14b41872 | Gerd Hoffmann | return -1; |
154 | 14b41872 | Gerd Hoffmann | return 0; |
155 | 14b41872 | Gerd Hoffmann | } |
156 | 14b41872 | Gerd Hoffmann | |
157 | 14b41872 | Gerd Hoffmann | static int print_drive(DeviceState *dev, Property *prop, char *dest, size_t len) |
158 | 14b41872 | Gerd Hoffmann | { |
159 | 14b41872 | Gerd Hoffmann | DriveInfo **ptr = qdev_get_prop_ptr(dev, prop); |
160 | 14b41872 | Gerd Hoffmann | return snprintf(dest, len, "%s", (*ptr)->id); |
161 | 14b41872 | Gerd Hoffmann | } |
162 | 14b41872 | Gerd Hoffmann | |
163 | 14b41872 | Gerd Hoffmann | PropertyInfo qdev_prop_drive = { |
164 | 14b41872 | Gerd Hoffmann | .name = "drive",
|
165 | 14b41872 | Gerd Hoffmann | .type = PROP_TYPE_DRIVE, |
166 | 14b41872 | Gerd Hoffmann | .size = sizeof(DriveInfo*),
|
167 | 14b41872 | Gerd Hoffmann | .parse = parse_drive, |
168 | 14b41872 | Gerd Hoffmann | .print = print_drive, |
169 | 14b41872 | Gerd Hoffmann | }; |
170 | 14b41872 | Gerd Hoffmann | |
171 | 313feaab | Gerd Hoffmann | /* --- character device --- */
|
172 | 313feaab | Gerd Hoffmann | |
173 | 313feaab | Gerd Hoffmann | static int print_chr(DeviceState *dev, Property *prop, char *dest, size_t len) |
174 | 313feaab | Gerd Hoffmann | { |
175 | 313feaab | Gerd Hoffmann | CharDriverState **ptr = qdev_get_prop_ptr(dev, prop); |
176 | bc19fcaa | Blue Swirl | |
177 | bc19fcaa | Blue Swirl | if (*ptr && (*ptr)->label) {
|
178 | bc19fcaa | Blue Swirl | return snprintf(dest, len, "%s", (*ptr)->label); |
179 | bc19fcaa | Blue Swirl | } else {
|
180 | bc19fcaa | Blue Swirl | return snprintf(dest, len, "<null>"); |
181 | bc19fcaa | Blue Swirl | } |
182 | 313feaab | Gerd Hoffmann | } |
183 | 313feaab | Gerd Hoffmann | |
184 | 313feaab | Gerd Hoffmann | PropertyInfo qdev_prop_chr = { |
185 | 313feaab | Gerd Hoffmann | .name = "chr",
|
186 | 313feaab | Gerd Hoffmann | .type = PROP_TYPE_CHR, |
187 | 313feaab | Gerd Hoffmann | .size = sizeof(CharDriverState*),
|
188 | 313feaab | Gerd Hoffmann | .print = print_chr, |
189 | 313feaab | Gerd Hoffmann | }; |
190 | 313feaab | Gerd Hoffmann | |
191 | ee6847d1 | Gerd Hoffmann | /* --- pointer --- */
|
192 | ee6847d1 | Gerd Hoffmann | |
193 | ee6847d1 | Gerd Hoffmann | static int print_ptr(DeviceState *dev, Property *prop, char *dest, size_t len) |
194 | ee6847d1 | Gerd Hoffmann | { |
195 | ee6847d1 | Gerd Hoffmann | void **ptr = qdev_get_prop_ptr(dev, prop);
|
196 | ee6847d1 | Gerd Hoffmann | return snprintf(dest, len, "<%p>", *ptr); |
197 | ee6847d1 | Gerd Hoffmann | } |
198 | ee6847d1 | Gerd Hoffmann | |
199 | ee6847d1 | Gerd Hoffmann | PropertyInfo qdev_prop_ptr = { |
200 | ee6847d1 | Gerd Hoffmann | .name = "ptr",
|
201 | ee6847d1 | Gerd Hoffmann | .type = PROP_TYPE_PTR, |
202 | ee6847d1 | Gerd Hoffmann | .size = sizeof(void*), |
203 | ee6847d1 | Gerd Hoffmann | .print = print_ptr, |
204 | ee6847d1 | Gerd Hoffmann | }; |
205 | ee6847d1 | Gerd Hoffmann | |
206 | ee6847d1 | Gerd Hoffmann | /* --- mac address --- */
|
207 | ee6847d1 | Gerd Hoffmann | |
208 | ee6847d1 | Gerd Hoffmann | /*
|
209 | ee6847d1 | Gerd Hoffmann | * accepted syntax versions:
|
210 | ee6847d1 | Gerd Hoffmann | * 01:02:03:04:05:06
|
211 | ee6847d1 | Gerd Hoffmann | * 01-02-03-04-05-06
|
212 | ee6847d1 | Gerd Hoffmann | */
|
213 | ee6847d1 | Gerd Hoffmann | static int parse_mac(DeviceState *dev, Property *prop, const char *str) |
214 | ee6847d1 | Gerd Hoffmann | { |
215 | ee6847d1 | Gerd Hoffmann | uint8_t *mac = qdev_get_prop_ptr(dev, prop); |
216 | ee6847d1 | Gerd Hoffmann | int i, pos;
|
217 | ee6847d1 | Gerd Hoffmann | char *p;
|
218 | ee6847d1 | Gerd Hoffmann | |
219 | ee6847d1 | Gerd Hoffmann | for (i = 0, pos = 0; i < 6; i++, pos += 3) { |
220 | 88e150a5 | Christoph Egger | if (!qemu_isxdigit(str[pos]))
|
221 | ee6847d1 | Gerd Hoffmann | return -1; |
222 | 88e150a5 | Christoph Egger | if (!qemu_isxdigit(str[pos+1])) |
223 | ee6847d1 | Gerd Hoffmann | return -1; |
224 | ee6847d1 | Gerd Hoffmann | if (i == 5 && str[pos+2] != '\0') |
225 | ee6847d1 | Gerd Hoffmann | return -1; |
226 | ee6847d1 | Gerd Hoffmann | if (str[pos+2] != ':' && str[pos+2] != '-') |
227 | ee6847d1 | Gerd Hoffmann | return -1; |
228 | ee6847d1 | Gerd Hoffmann | mac[i] = strtol(str+pos, &p, 16);
|
229 | ee6847d1 | Gerd Hoffmann | } |
230 | ee6847d1 | Gerd Hoffmann | return 0; |
231 | ee6847d1 | Gerd Hoffmann | } |
232 | ee6847d1 | Gerd Hoffmann | |
233 | ee6847d1 | Gerd Hoffmann | static int print_mac(DeviceState *dev, Property *prop, char *dest, size_t len) |
234 | ee6847d1 | Gerd Hoffmann | { |
235 | ee6847d1 | Gerd Hoffmann | uint8_t *mac = qdev_get_prop_ptr(dev, prop); |
236 | ee6847d1 | Gerd Hoffmann | return snprintf(dest, len, "%02x:%02x:%02x:%02x:%02x:%02x", |
237 | ee6847d1 | Gerd Hoffmann | mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); |
238 | ee6847d1 | Gerd Hoffmann | } |
239 | ee6847d1 | Gerd Hoffmann | |
240 | ee6847d1 | Gerd Hoffmann | PropertyInfo qdev_prop_macaddr = { |
241 | ee6847d1 | Gerd Hoffmann | .name = "mac-addr",
|
242 | ee6847d1 | Gerd Hoffmann | .type = PROP_TYPE_MACADDR, |
243 | ee6847d1 | Gerd Hoffmann | .size = 6,
|
244 | ee6847d1 | Gerd Hoffmann | .parse = parse_mac, |
245 | ee6847d1 | Gerd Hoffmann | .print = print_mac, |
246 | ee6847d1 | Gerd Hoffmann | }; |
247 | ee6847d1 | Gerd Hoffmann | |
248 | 05cb5fe4 | Gerd Hoffmann | /* --- pci address --- */
|
249 | 05cb5fe4 | Gerd Hoffmann | |
250 | 05cb5fe4 | Gerd Hoffmann | /*
|
251 | 05cb5fe4 | Gerd Hoffmann | * bus-local address, i.e. "$slot" or "$slot.$fn"
|
252 | 05cb5fe4 | Gerd Hoffmann | */
|
253 | 05cb5fe4 | Gerd Hoffmann | static int parse_pci_devfn(DeviceState *dev, Property *prop, const char *str) |
254 | 05cb5fe4 | Gerd Hoffmann | { |
255 | 05cb5fe4 | Gerd Hoffmann | uint32_t *ptr = qdev_get_prop_ptr(dev, prop); |
256 | 05cb5fe4 | Gerd Hoffmann | unsigned int slot, fn, n; |
257 | 05cb5fe4 | Gerd Hoffmann | |
258 | 05cb5fe4 | Gerd Hoffmann | if (sscanf(str, "%x.%x%n", &slot, &fn, &n) != 2) { |
259 | 05cb5fe4 | Gerd Hoffmann | fn = 0;
|
260 | 05cb5fe4 | Gerd Hoffmann | if (sscanf(str, "%x%n", &slot, &n) != 1) { |
261 | 05cb5fe4 | Gerd Hoffmann | return -1; |
262 | 05cb5fe4 | Gerd Hoffmann | } |
263 | 05cb5fe4 | Gerd Hoffmann | } |
264 | 05cb5fe4 | Gerd Hoffmann | if (str[n] != '\0') |
265 | 05cb5fe4 | Gerd Hoffmann | return -1; |
266 | 05cb5fe4 | Gerd Hoffmann | if (fn > 7) |
267 | 05cb5fe4 | Gerd Hoffmann | return -1; |
268 | 05cb5fe4 | Gerd Hoffmann | *ptr = slot << 3 | fn;
|
269 | 05cb5fe4 | Gerd Hoffmann | return 0; |
270 | 05cb5fe4 | Gerd Hoffmann | } |
271 | 05cb5fe4 | Gerd Hoffmann | |
272 | 05cb5fe4 | Gerd Hoffmann | static int print_pci_devfn(DeviceState *dev, Property *prop, char *dest, size_t len) |
273 | 05cb5fe4 | Gerd Hoffmann | { |
274 | 05cb5fe4 | Gerd Hoffmann | uint32_t *ptr = qdev_get_prop_ptr(dev, prop); |
275 | 05cb5fe4 | Gerd Hoffmann | |
276 | 05cb5fe4 | Gerd Hoffmann | if (-1 == *ptr) { |
277 | 05cb5fe4 | Gerd Hoffmann | return snprintf(dest, len, "<unset>"); |
278 | 05cb5fe4 | Gerd Hoffmann | } else {
|
279 | 05cb5fe4 | Gerd Hoffmann | return snprintf(dest, len, "%02x.%x", *ptr >> 3, *ptr & 7); |
280 | 05cb5fe4 | Gerd Hoffmann | } |
281 | 05cb5fe4 | Gerd Hoffmann | } |
282 | 05cb5fe4 | Gerd Hoffmann | |
283 | 05cb5fe4 | Gerd Hoffmann | PropertyInfo qdev_prop_pci_devfn = { |
284 | 05cb5fe4 | Gerd Hoffmann | .name = "pci-devfn",
|
285 | 05cb5fe4 | Gerd Hoffmann | .type = PROP_TYPE_UINT32, |
286 | 05cb5fe4 | Gerd Hoffmann | .size = sizeof(uint32_t),
|
287 | 05cb5fe4 | Gerd Hoffmann | .parse = parse_pci_devfn, |
288 | 05cb5fe4 | Gerd Hoffmann | .print = print_pci_devfn, |
289 | 05cb5fe4 | Gerd Hoffmann | }; |
290 | 05cb5fe4 | Gerd Hoffmann | |
291 | ee6847d1 | Gerd Hoffmann | /* --- public helpers --- */
|
292 | ee6847d1 | Gerd Hoffmann | |
293 | ee6847d1 | Gerd Hoffmann | static Property *qdev_prop_walk(Property *props, const char *name) |
294 | ee6847d1 | Gerd Hoffmann | { |
295 | ee6847d1 | Gerd Hoffmann | if (!props)
|
296 | ee6847d1 | Gerd Hoffmann | return NULL; |
297 | ee6847d1 | Gerd Hoffmann | while (props->name) {
|
298 | ee6847d1 | Gerd Hoffmann | if (strcmp(props->name, name) == 0) |
299 | ee6847d1 | Gerd Hoffmann | return props;
|
300 | ee6847d1 | Gerd Hoffmann | props++; |
301 | ee6847d1 | Gerd Hoffmann | } |
302 | ee6847d1 | Gerd Hoffmann | return NULL; |
303 | ee6847d1 | Gerd Hoffmann | } |
304 | ee6847d1 | Gerd Hoffmann | |
305 | ee6847d1 | Gerd Hoffmann | static Property *qdev_prop_find(DeviceState *dev, const char *name) |
306 | ee6847d1 | Gerd Hoffmann | { |
307 | ee6847d1 | Gerd Hoffmann | Property *prop; |
308 | ee6847d1 | Gerd Hoffmann | |
309 | ee6847d1 | Gerd Hoffmann | /* device properties */
|
310 | ee6847d1 | Gerd Hoffmann | prop = qdev_prop_walk(dev->info->props, name); |
311 | ee6847d1 | Gerd Hoffmann | if (prop)
|
312 | ee6847d1 | Gerd Hoffmann | return prop;
|
313 | ee6847d1 | Gerd Hoffmann | |
314 | ee6847d1 | Gerd Hoffmann | /* bus properties */
|
315 | ee6847d1 | Gerd Hoffmann | prop = qdev_prop_walk(dev->parent_bus->info->props, name); |
316 | ee6847d1 | Gerd Hoffmann | if (prop)
|
317 | ee6847d1 | Gerd Hoffmann | return prop;
|
318 | ee6847d1 | Gerd Hoffmann | |
319 | ee6847d1 | Gerd Hoffmann | return NULL; |
320 | ee6847d1 | Gerd Hoffmann | } |
321 | ee6847d1 | Gerd Hoffmann | |
322 | ee6847d1 | Gerd Hoffmann | int qdev_prop_parse(DeviceState *dev, const char *name, const char *value) |
323 | ee6847d1 | Gerd Hoffmann | { |
324 | ee6847d1 | Gerd Hoffmann | Property *prop; |
325 | ee6847d1 | Gerd Hoffmann | |
326 | ee6847d1 | Gerd Hoffmann | prop = qdev_prop_find(dev, name); |
327 | ee6847d1 | Gerd Hoffmann | if (!prop) {
|
328 | ee6847d1 | Gerd Hoffmann | fprintf(stderr, "property \"%s.%s\" not found\n",
|
329 | ee6847d1 | Gerd Hoffmann | dev->info->name, name); |
330 | ee6847d1 | Gerd Hoffmann | return -1; |
331 | ee6847d1 | Gerd Hoffmann | } |
332 | ee6847d1 | Gerd Hoffmann | if (!prop->info->parse) {
|
333 | ee6847d1 | Gerd Hoffmann | fprintf(stderr, "property \"%s.%s\" has no parser\n",
|
334 | ee6847d1 | Gerd Hoffmann | dev->info->name, name); |
335 | ee6847d1 | Gerd Hoffmann | return -1; |
336 | ee6847d1 | Gerd Hoffmann | } |
337 | ee6847d1 | Gerd Hoffmann | return prop->info->parse(dev, prop, value);
|
338 | ee6847d1 | Gerd Hoffmann | } |
339 | ee6847d1 | Gerd Hoffmann | |
340 | ee6847d1 | Gerd Hoffmann | void qdev_prop_set(DeviceState *dev, const char *name, void *src, enum PropertyType type) |
341 | ee6847d1 | Gerd Hoffmann | { |
342 | ee6847d1 | Gerd Hoffmann | Property *prop; |
343 | ee6847d1 | Gerd Hoffmann | void *dst;
|
344 | ee6847d1 | Gerd Hoffmann | |
345 | ee6847d1 | Gerd Hoffmann | prop = qdev_prop_find(dev, name); |
346 | ee6847d1 | Gerd Hoffmann | if (!prop) {
|
347 | ee6847d1 | Gerd Hoffmann | fprintf(stderr, "%s: property \"%s.%s\" not found\n",
|
348 | ee6847d1 | Gerd Hoffmann | __FUNCTION__, dev->info->name, name); |
349 | ee6847d1 | Gerd Hoffmann | abort(); |
350 | ee6847d1 | Gerd Hoffmann | } |
351 | ee6847d1 | Gerd Hoffmann | if (prop->info->type != type) {
|
352 | ee6847d1 | Gerd Hoffmann | fprintf(stderr, "%s: property \"%s.%s\" type mismatch\n",
|
353 | ee6847d1 | Gerd Hoffmann | __FUNCTION__, dev->info->name, name); |
354 | ee6847d1 | Gerd Hoffmann | abort(); |
355 | ee6847d1 | Gerd Hoffmann | } |
356 | ee6847d1 | Gerd Hoffmann | dst = qdev_get_prop_ptr(dev, prop); |
357 | ee6847d1 | Gerd Hoffmann | memcpy(dst, src, prop->info->size); |
358 | ee6847d1 | Gerd Hoffmann | } |
359 | ee6847d1 | Gerd Hoffmann | |
360 | ee6847d1 | Gerd Hoffmann | void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value) |
361 | ee6847d1 | Gerd Hoffmann | { |
362 | ee6847d1 | Gerd Hoffmann | qdev_prop_set(dev, name, &value, PROP_TYPE_UINT16); |
363 | ee6847d1 | Gerd Hoffmann | } |
364 | ee6847d1 | Gerd Hoffmann | |
365 | ee6847d1 | Gerd Hoffmann | void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value) |
366 | ee6847d1 | Gerd Hoffmann | { |
367 | ee6847d1 | Gerd Hoffmann | qdev_prop_set(dev, name, &value, PROP_TYPE_UINT32); |
368 | ee6847d1 | Gerd Hoffmann | } |
369 | ee6847d1 | Gerd Hoffmann | |
370 | 5a053d1f | Blue Swirl | void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value) |
371 | 5a053d1f | Blue Swirl | { |
372 | 5a053d1f | Blue Swirl | qdev_prop_set(dev, name, &value, PROP_TYPE_UINT64); |
373 | 5a053d1f | Blue Swirl | } |
374 | 5a053d1f | Blue Swirl | |
375 | 14b41872 | Gerd Hoffmann | void qdev_prop_set_drive(DeviceState *dev, const char *name, DriveInfo *value) |
376 | 14b41872 | Gerd Hoffmann | { |
377 | 14b41872 | Gerd Hoffmann | qdev_prop_set(dev, name, &value, PROP_TYPE_DRIVE); |
378 | 14b41872 | Gerd Hoffmann | } |
379 | 14b41872 | Gerd Hoffmann | |
380 | 313feaab | Gerd Hoffmann | void qdev_prop_set_chr(DeviceState *dev, const char *name, CharDriverState *value) |
381 | 313feaab | Gerd Hoffmann | { |
382 | 313feaab | Gerd Hoffmann | qdev_prop_set(dev, name, &value, PROP_TYPE_CHR); |
383 | 313feaab | Gerd Hoffmann | } |
384 | 313feaab | Gerd Hoffmann | |
385 | ee6847d1 | Gerd Hoffmann | void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value) |
386 | ee6847d1 | Gerd Hoffmann | { |
387 | ee6847d1 | Gerd Hoffmann | qdev_prop_set(dev, name, &value, PROP_TYPE_PTR); |
388 | ee6847d1 | Gerd Hoffmann | } |
389 | ee6847d1 | Gerd Hoffmann | |
390 | ee6847d1 | Gerd Hoffmann | void qdev_prop_set_defaults(DeviceState *dev, Property *props)
|
391 | ee6847d1 | Gerd Hoffmann | { |
392 | ee6847d1 | Gerd Hoffmann | char *dst;
|
393 | ee6847d1 | Gerd Hoffmann | |
394 | ee6847d1 | Gerd Hoffmann | if (!props)
|
395 | ee6847d1 | Gerd Hoffmann | return;
|
396 | ee6847d1 | Gerd Hoffmann | while (props->name) {
|
397 | ee6847d1 | Gerd Hoffmann | if (props->defval) {
|
398 | ee6847d1 | Gerd Hoffmann | dst = qdev_get_prop_ptr(dev, props); |
399 | ee6847d1 | Gerd Hoffmann | memcpy(dst, props->defval, props->info->size); |
400 | ee6847d1 | Gerd Hoffmann | } |
401 | ee6847d1 | Gerd Hoffmann | props++; |
402 | ee6847d1 | Gerd Hoffmann | } |
403 | ee6847d1 | Gerd Hoffmann | } |
404 | ee6847d1 | Gerd Hoffmann | |
405 | b6b61144 | Gerd Hoffmann | static CompatProperty *compat_props;
|
406 | b6b61144 | Gerd Hoffmann | |
407 | b6b61144 | Gerd Hoffmann | void qdev_prop_register_compat(CompatProperty *props)
|
408 | b6b61144 | Gerd Hoffmann | { |
409 | b6b61144 | Gerd Hoffmann | compat_props = props; |
410 | b6b61144 | Gerd Hoffmann | } |
411 | b6b61144 | Gerd Hoffmann | |
412 | b6b61144 | Gerd Hoffmann | void qdev_prop_set_compat(DeviceState *dev)
|
413 | b6b61144 | Gerd Hoffmann | { |
414 | b6b61144 | Gerd Hoffmann | CompatProperty *prop; |
415 | b6b61144 | Gerd Hoffmann | |
416 | b6b61144 | Gerd Hoffmann | if (!compat_props) {
|
417 | b6b61144 | Gerd Hoffmann | return;
|
418 | b6b61144 | Gerd Hoffmann | } |
419 | b6b61144 | Gerd Hoffmann | for (prop = compat_props; prop->driver != NULL; prop++) { |
420 | b6b61144 | Gerd Hoffmann | if (strcmp(dev->info->name, prop->driver) != 0) { |
421 | b6b61144 | Gerd Hoffmann | continue;
|
422 | b6b61144 | Gerd Hoffmann | } |
423 | b6b61144 | Gerd Hoffmann | if (qdev_prop_parse(dev, prop->property, prop->value) != 0) { |
424 | b6b61144 | Gerd Hoffmann | abort(); |
425 | b6b61144 | Gerd Hoffmann | } |
426 | b6b61144 | Gerd Hoffmann | } |
427 | b6b61144 | Gerd Hoffmann | } |