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