Statistics
| Branch: | Revision:

root / hw / qdev.c @ 036f7166

History | View | Annotate | Download (20.9 kB)

1
/*
2
 *  Dynamic device configuration and creation.
3
 *
4
 *  Copyright (c) 2009 CodeSourcery
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18
 */
19

    
20
/* The theory here is that it should be possible to create a machine without
21
   knowledge of specific devices.  Historically board init routines have
22
   passed a bunch of arguments to each device, requiring the board know
23
   exactly which device it is dealing with.  This file provides an abstract
24
   API for device configuration and initialization.  Devices will generally
25
   inherit from a particular bus (e.g. PCI or I2C) rather than
26
   this API directly.  */
27

    
28
#include "net.h"
29
#include "qdev.h"
30
#include "sysemu.h"
31
#include "monitor.h"
32
#include "qerror.h"
33

    
34
static int qdev_hotplug = 0;
35

    
36
/* This is a nasty hack to allow passing a NULL bus to qdev_create.  */
37
static BusState *main_system_bus;
38

    
39
DeviceInfo *device_info_list;
40

    
41
static BusState *qbus_find_recursive(BusState *bus, const char *name,
42
                                     const BusInfo *info);
43
static BusState *qbus_find(const char *path);
44

    
45
/* Register a new device type.  */
46
void qdev_register(DeviceInfo *info)
47
{
48
    assert(info->size >= sizeof(DeviceState));
49
    assert(!info->next);
50

    
51
    info->next = device_info_list;
52
    device_info_list = info;
53
}
54

    
55
static DeviceInfo *qdev_find_info(BusInfo *bus_info, const char *name)
56
{
57
    DeviceInfo *info;
58

    
59
    /* first check device names */
60
    for (info = device_info_list; info != NULL; info = info->next) {
61
        if (bus_info && info->bus_info != bus_info)
62
            continue;
63
        if (strcmp(info->name, name) != 0)
64
            continue;
65
        return info;
66
    }
67

    
68
    /* failing that check the aliases */
69
    for (info = device_info_list; info != NULL; info = info->next) {
70
        if (bus_info && info->bus_info != bus_info)
71
            continue;
72
        if (!info->alias)
73
            continue;
74
        if (strcmp(info->alias, name) != 0)
75
            continue;
76
        return info;
77
    }
78
    return NULL;
79
}
80

    
81
static DeviceState *qdev_create_from_info(BusState *bus, DeviceInfo *info)
82
{
83
    DeviceState *dev;
84

    
85
    assert(bus->info == info->bus_info);
86
    dev = qemu_mallocz(info->size);
87
    dev->info = info;
88
    dev->parent_bus = bus;
89
    qdev_prop_set_defaults(dev, dev->info->props);
90
    qdev_prop_set_defaults(dev, dev->parent_bus->info->props);
91
    qdev_prop_set_globals(dev);
92
    QLIST_INSERT_HEAD(&bus->children, dev, sibling);
93
    if (qdev_hotplug) {
94
        assert(bus->allow_hotplug);
95
        dev->hotplugged = 1;
96
    }
97
    dev->state = DEV_STATE_CREATED;
98
    return dev;
99
}
100

    
101
/* Create a new device.  This only initializes the device state structure
102
   and allows properties to be set.  qdev_init should be called to
103
   initialize the actual device emulation.  */
104
DeviceState *qdev_create(BusState *bus, const char *name)
105
{
106
    DeviceInfo *info;
107

    
108
    if (!bus) {
109
        if (!main_system_bus) {
110
            main_system_bus = qbus_create(&system_bus_info, NULL, "main-system-bus");
111
        }
112
        bus = main_system_bus;
113
    }
114

    
115
    info = qdev_find_info(bus->info, name);
116
    if (!info) {
117
        hw_error("Unknown device '%s' for bus '%s'\n", name, bus->info->name);
118
    }
119

    
120
    return qdev_create_from_info(bus, info);
121
}
122

    
123
static void qdev_print_devinfo(DeviceInfo *info)
124
{
125
    error_printf("name \"%s\", bus %s",
126
                 info->name, info->bus_info->name);
127
    if (info->alias) {
128
        error_printf(", alias \"%s\"", info->alias);
129
    }
130
    if (info->desc) {
131
        error_printf(", desc \"%s\"", info->desc);
132
    }
133
    if (info->no_user) {
134
        error_printf(", no-user");
135
    }
136
    error_printf("\n");
137
}
138

    
139
static int set_property(const char *name, const char *value, void *opaque)
140
{
141
    DeviceState *dev = opaque;
142

    
143
    if (strcmp(name, "driver") == 0)
144
        return 0;
145
    if (strcmp(name, "bus") == 0)
146
        return 0;
147

    
148
    if (qdev_prop_parse(dev, name, value) == -1) {
149
        error_report("can't set property \"%s\" to \"%s\" for \"%s\"",
150
                     name, value, dev->info->name);
151
        return -1;
152
    }
153
    return 0;
154
}
155

    
156
int qdev_device_help(QemuOpts *opts)
157
{
158
    const char *driver;
159
    DeviceInfo *info;
160
    Property *prop;
161

    
162
    driver = qemu_opt_get(opts, "driver");
163
    if (driver && !strcmp(driver, "?")) {
164
        for (info = device_info_list; info != NULL; info = info->next) {
165
            if (info->no_user) {
166
                continue;       /* not available, don't show */
167
            }
168
            qdev_print_devinfo(info);
169
        }
170
        return 1;
171
    }
172

    
173
    if (!qemu_opt_get(opts, "?")) {
174
        return 0;
175
    }
176

    
177
    info = qdev_find_info(NULL, driver);
178
    if (!info) {
179
        return 0;
180
    }
181

    
182
    for (prop = info->props; prop && prop->name; prop++) {
183
        /*
184
         * TODO Properties without a parser are just for dirty hacks.
185
         * qdev_prop_ptr is the only such PropertyInfo.  It's marked
186
         * for removal.  This conditional should be removed along with
187
         * it.
188
         */
189
        if (!prop->info->parse) {
190
            continue;           /* no way to set it, don't show */
191
        }
192
        error_printf("%s.%s=%s\n", info->name, prop->name, prop->info->name);
193
    }
194
    return 1;
195
}
196

    
197
DeviceState *qdev_device_add(QemuOpts *opts)
198
{
199
    const char *driver, *path, *id;
200
    DeviceInfo *info;
201
    DeviceState *qdev;
202
    BusState *bus;
203

    
204
    driver = qemu_opt_get(opts, "driver");
205
    if (!driver) {
206
        error_report("-device: no driver specified");
207
        return NULL;
208
    }
209

    
210
    /* find driver */
211
    info = qdev_find_info(NULL, driver);
212
    if (!info || info->no_user) {
213
        qerror_report(QERR_DEVICE_NOT_FOUND, driver);
214
        return NULL;
215
    }
216

    
217
    /* find bus */
218
    path = qemu_opt_get(opts, "bus");
219
    if (path != NULL) {
220
        bus = qbus_find(path);
221
        if (bus && bus->info != info->bus_info) {
222
            error_report("Device '%s' can't go on a %s bus",
223
                         driver, bus->info->name);
224
            return NULL;
225
        }
226
    } else {
227
        bus = qbus_find_recursive(main_system_bus, NULL, info->bus_info);
228
    }
229
    if (!bus) {
230
        error_report("Did not find %s bus for %s",
231
                     path ? path : info->bus_info->name, info->name);
232
        return NULL;
233
    }
234
    if (qdev_hotplug && !bus->allow_hotplug) {
235
        error_report("Bus %s does not support hotplugging",
236
                     bus->name);
237
        return NULL;
238
    }
239

    
240
    /* create device, set properties */
241
    qdev = qdev_create_from_info(bus, info);
242
    id = qemu_opts_id(opts);
243
    if (id) {
244
        qdev->id = id;
245
    }
246
    if (qemu_opt_foreach(opts, set_property, qdev, 1) != 0) {
247
        qdev_free(qdev);
248
        return NULL;
249
    }
250
    if (qdev_init(qdev) < 0) {
251
        error_report("Error initializing device %s", driver);
252
        return NULL;
253
    }
254
    qdev->opts = opts;
255
    return qdev;
256
}
257

    
258
static void qdev_reset(void *opaque)
259
{
260
    DeviceState *dev = opaque;
261
    if (dev->info->reset)
262
        dev->info->reset(dev);
263
}
264

    
265
/* Initialize a device.  Device properties should be set before calling
266
   this function.  IRQs and MMIO regions should be connected/mapped after
267
   calling this function.
268
   On failure, destroy the device and return negative value.
269
   Return 0 on success.  */
270
int qdev_init(DeviceState *dev)
271
{
272
    int rc;
273

    
274
    assert(dev->state == DEV_STATE_CREATED);
275
    rc = dev->info->init(dev, dev->info);
276
    if (rc < 0) {
277
        qdev_free(dev);
278
        return rc;
279
    }
280
    qemu_register_reset(qdev_reset, dev);
281
    if (dev->info->vmsd)
282
        vmstate_register(-1, dev->info->vmsd, dev);
283
    dev->state = DEV_STATE_INITIALIZED;
284
    return 0;
285
}
286

    
287
int qdev_unplug(DeviceState *dev)
288
{
289
    if (!dev->parent_bus->allow_hotplug) {
290
        error_report("Bus %s does not support hotplugging",
291
                     dev->parent_bus->name);
292
        return -1;
293
    }
294
    assert(dev->info->unplug != NULL);
295

    
296
    return dev->info->unplug(dev);
297
}
298

    
299
/* can be used as ->unplug() callback for the simple cases */
300
int qdev_simple_unplug_cb(DeviceState *dev)
301
{
302
    /* just zap it */
303
    qdev_free(dev);
304
    return 0;
305
}
306

    
307
/* Like qdev_init(), but terminate program via hw_error() instead of
308
   returning an error value.  This is okay during machine creation.
309
   Don't use for hotplug, because there callers need to recover from
310
   failure.  Exception: if you know the device's init() callback can't
311
   fail, then qdev_init_nofail() can't fail either, and is therefore
312
   usable even then.  But relying on the device implementation that
313
   way is somewhat unclean, and best avoided.  */
314
void qdev_init_nofail(DeviceState *dev)
315
{
316
    DeviceInfo *info = dev->info;
317

    
318
    if (qdev_init(dev) < 0)
319
        hw_error("Initialization of device %s failed\n", info->name);
320
}
321

    
322
/* Unlink device from bus and free the structure.  */
323
void qdev_free(DeviceState *dev)
324
{
325
    BusState *bus;
326

    
327
    if (dev->state == DEV_STATE_INITIALIZED) {
328
        while (dev->num_child_bus) {
329
            bus = QLIST_FIRST(&dev->child_bus);
330
            qbus_free(bus);
331
        }
332
        if (dev->info->vmsd)
333
            vmstate_unregister(dev->info->vmsd, dev);
334
        if (dev->info->exit)
335
            dev->info->exit(dev);
336
        if (dev->opts)
337
            qemu_opts_del(dev->opts);
338
    }
339
    qemu_unregister_reset(qdev_reset, dev);
340
    QLIST_REMOVE(dev, sibling);
341
    qemu_free(dev);
342
}
343

    
344
void qdev_machine_creation_done(void)
345
{
346
    /*
347
     * ok, initial machine setup is done, starting from now we can
348
     * only create hotpluggable devices
349
     */
350
    qdev_hotplug = 1;
351
}
352

    
353
/* Get a character (serial) device interface.  */
354
CharDriverState *qdev_init_chardev(DeviceState *dev)
355
{
356
    static int next_serial;
357

    
358
    /* FIXME: This function needs to go away: use chardev properties!  */
359
    return serial_hds[next_serial++];
360
}
361

    
362
BusState *qdev_get_parent_bus(DeviceState *dev)
363
{
364
    return dev->parent_bus;
365
}
366

    
367
void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
368
{
369
    assert(dev->num_gpio_in == 0);
370
    dev->num_gpio_in = n;
371
    dev->gpio_in = qemu_allocate_irqs(handler, dev, n);
372
}
373

    
374
void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
375
{
376
    assert(dev->num_gpio_out == 0);
377
    dev->num_gpio_out = n;
378
    dev->gpio_out = pins;
379
}
380

    
381
qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
382
{
383
    assert(n >= 0 && n < dev->num_gpio_in);
384
    return dev->gpio_in[n];
385
}
386

    
387
void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
388
{
389
    assert(n >= 0 && n < dev->num_gpio_out);
390
    dev->gpio_out[n] = pin;
391
}
392

    
393
void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd)
394
{
395
    qdev_prop_set_macaddr(dev, "mac", nd->macaddr);
396
    if (nd->vlan)
397
        qdev_prop_set_vlan(dev, "vlan", nd->vlan);
398
    if (nd->netdev)
399
        qdev_prop_set_netdev(dev, "netdev", nd->netdev);
400
    if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
401
        qdev_prop_exists(dev, "vectors")) {
402
        qdev_prop_set_uint32(dev, "vectors", nd->nvectors);
403
    }
404
}
405

    
406
static int next_block_unit[IF_COUNT];
407

    
408
/* Get a block device.  This should only be used for single-drive devices
409
   (e.g. SD/Floppy/MTD).  Multi-disk devices (scsi/ide) should use the
410
   appropriate bus.  */
411
BlockDriverState *qdev_init_bdrv(DeviceState *dev, BlockInterfaceType type)
412
{
413
    int unit = next_block_unit[type]++;
414
    DriveInfo *dinfo;
415

    
416
    dinfo = drive_get(type, 0, unit);
417
    return dinfo ? dinfo->bdrv : NULL;
418
}
419

    
420
BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
421
{
422
    BusState *bus;
423

    
424
    QLIST_FOREACH(bus, &dev->child_bus, sibling) {
425
        if (strcmp(name, bus->name) == 0) {
426
            return bus;
427
        }
428
    }
429
    return NULL;
430
}
431

    
432
static BusState *qbus_find_recursive(BusState *bus, const char *name,
433
                                     const BusInfo *info)
434
{
435
    DeviceState *dev;
436
    BusState *child, *ret;
437
    int match = 1;
438

    
439
    if (name && (strcmp(bus->name, name) != 0)) {
440
        match = 0;
441
    }
442
    if (info && (bus->info != info)) {
443
        match = 0;
444
    }
445
    if (match) {
446
        return bus;
447
    }
448

    
449
    QLIST_FOREACH(dev, &bus->children, sibling) {
450
        QLIST_FOREACH(child, &dev->child_bus, sibling) {
451
            ret = qbus_find_recursive(child, name, info);
452
            if (ret) {
453
                return ret;
454
            }
455
        }
456
    }
457
    return NULL;
458
}
459

    
460
static DeviceState *qdev_find_recursive(BusState *bus, const char *id)
461
{
462
    DeviceState *dev, *ret;
463
    BusState *child;
464

    
465
    QLIST_FOREACH(dev, &bus->children, sibling) {
466
        if (dev->id && strcmp(dev->id, id) == 0)
467
            return dev;
468
        QLIST_FOREACH(child, &dev->child_bus, sibling) {
469
            ret = qdev_find_recursive(child, id);
470
            if (ret) {
471
                return ret;
472
            }
473
        }
474
    }
475
    return NULL;
476
}
477

    
478
static void qbus_list_bus(DeviceState *dev)
479
{
480
    BusState *child;
481
    const char *sep = " ";
482

    
483
    error_printf("child busses at \"%s\":",
484
                 dev->id ? dev->id : dev->info->name);
485
    QLIST_FOREACH(child, &dev->child_bus, sibling) {
486
        error_printf("%s\"%s\"", sep, child->name);
487
        sep = ", ";
488
    }
489
    error_printf("\n");
490
}
491

    
492
static void qbus_list_dev(BusState *bus)
493
{
494
    DeviceState *dev;
495
    const char *sep = " ";
496

    
497
    error_printf("devices at \"%s\":", bus->name);
498
    QLIST_FOREACH(dev, &bus->children, sibling) {
499
        error_printf("%s\"%s\"", sep, dev->info->name);
500
        if (dev->id)
501
            error_printf("/\"%s\"", dev->id);
502
        sep = ", ";
503
    }
504
    error_printf("\n");
505
}
506

    
507
static BusState *qbus_find_bus(DeviceState *dev, char *elem)
508
{
509
    BusState *child;
510

    
511
    QLIST_FOREACH(child, &dev->child_bus, sibling) {
512
        if (strcmp(child->name, elem) == 0) {
513
            return child;
514
        }
515
    }
516
    return NULL;
517
}
518

    
519
static DeviceState *qbus_find_dev(BusState *bus, char *elem)
520
{
521
    DeviceState *dev;
522

    
523
    /*
524
     * try to match in order:
525
     *   (1) instance id, if present
526
     *   (2) driver name
527
     *   (3) driver alias, if present
528
     */
529
    QLIST_FOREACH(dev, &bus->children, sibling) {
530
        if (dev->id  &&  strcmp(dev->id, elem) == 0) {
531
            return dev;
532
        }
533
    }
534
    QLIST_FOREACH(dev, &bus->children, sibling) {
535
        if (strcmp(dev->info->name, elem) == 0) {
536
            return dev;
537
        }
538
    }
539
    QLIST_FOREACH(dev, &bus->children, sibling) {
540
        if (dev->info->alias && strcmp(dev->info->alias, elem) == 0) {
541
            return dev;
542
        }
543
    }
544
    return NULL;
545
}
546

    
547
static BusState *qbus_find(const char *path)
548
{
549
    DeviceState *dev;
550
    BusState *bus;
551
    char elem[128];
552
    int pos, len;
553

    
554
    /* find start element */
555
    if (path[0] == '/') {
556
        bus = main_system_bus;
557
        pos = 0;
558
    } else {
559
        if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
560
            error_report("path parse error (\"%s\")", path);
561
            return NULL;
562
        }
563
        bus = qbus_find_recursive(main_system_bus, elem, NULL);
564
        if (!bus) {
565
            error_report("bus \"%s\" not found", elem);
566
            return NULL;
567
        }
568
        pos = len;
569
    }
570

    
571
    for (;;) {
572
        if (path[pos] == '\0') {
573
            /* we are done */
574
            return bus;
575
        }
576

    
577
        /* find device */
578
        if (sscanf(path+pos, "/%127[^/]%n", elem, &len) != 1) {
579
            error_report("path parse error (\"%s\" pos %d)", path, pos);
580
            return NULL;
581
        }
582
        pos += len;
583
        dev = qbus_find_dev(bus, elem);
584
        if (!dev) {
585
            error_report("device \"%s\" not found", elem);
586
            qbus_list_dev(bus);
587
            return NULL;
588
        }
589
        if (path[pos] == '\0') {
590
            /* last specified element is a device.  If it has exactly
591
             * one child bus accept it nevertheless */
592
            switch (dev->num_child_bus) {
593
            case 0:
594
                error_report("device has no child bus (%s)", path);
595
                return NULL;
596
            case 1:
597
                return QLIST_FIRST(&dev->child_bus);
598
            default:
599
                error_report("device has multiple child busses (%s)", path);
600
                qbus_list_bus(dev);
601
                return NULL;
602
            }
603
        }
604

    
605
        /* find bus */
606
        if (sscanf(path+pos, "/%127[^/]%n", elem, &len) != 1) {
607
            error_report("path parse error (\"%s\" pos %d)", path, pos);
608
            return NULL;
609
        }
610
        pos += len;
611
        bus = qbus_find_bus(dev, elem);
612
        if (!bus) {
613
            error_report("child bus \"%s\" not found", elem);
614
            qbus_list_bus(dev);
615
            return NULL;
616
        }
617
    }
618
}
619

    
620
void qbus_create_inplace(BusState *bus, BusInfo *info,
621
                         DeviceState *parent, const char *name)
622
{
623
    char *buf;
624
    int i,len;
625

    
626
    bus->info = info;
627
    bus->parent = parent;
628

    
629
    if (name) {
630
        /* use supplied name */
631
        bus->name = qemu_strdup(name);
632
    } else if (parent && parent->id) {
633
        /* parent device has id -> use it for bus name */
634
        len = strlen(parent->id) + 16;
635
        buf = qemu_malloc(len);
636
        snprintf(buf, len, "%s.%d", parent->id, parent->num_child_bus);
637
        bus->name = buf;
638
    } else {
639
        /* no id -> use lowercase bus type for bus name */
640
        len = strlen(info->name) + 16;
641
        buf = qemu_malloc(len);
642
        len = snprintf(buf, len, "%s.%d", info->name,
643
                       parent ? parent->num_child_bus : 0);
644
        for (i = 0; i < len; i++)
645
            buf[i] = qemu_tolower(buf[i]);
646
        bus->name = buf;
647
    }
648

    
649
    QLIST_INIT(&bus->children);
650
    if (parent) {
651
        QLIST_INSERT_HEAD(&parent->child_bus, bus, sibling);
652
        parent->num_child_bus++;
653
    }
654

    
655
}
656

    
657
BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name)
658
{
659
    BusState *bus;
660

    
661
    bus = qemu_mallocz(info->size);
662
    bus->qdev_allocated = 1;
663
    qbus_create_inplace(bus, info, parent, name);
664
    return bus;
665
}
666

    
667
void qbus_free(BusState *bus)
668
{
669
    DeviceState *dev;
670

    
671
    while ((dev = QLIST_FIRST(&bus->children)) != NULL) {
672
        qdev_free(dev);
673
    }
674
    if (bus->parent) {
675
        QLIST_REMOVE(bus, sibling);
676
        bus->parent->num_child_bus--;
677
    }
678
    if (bus->qdev_allocated) {
679
        qemu_free(bus);
680
    }
681
}
682

    
683
#define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
684
static void qbus_print(Monitor *mon, BusState *bus, int indent);
685

    
686
static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
687
                             const char *prefix, int indent)
688
{
689
    char buf[64];
690

    
691
    if (!props)
692
        return;
693
    while (props->name) {
694
        /*
695
         * TODO Properties without a print method are just for dirty
696
         * hacks.  qdev_prop_ptr is the only such PropertyInfo.  It's
697
         * marked for removal.  The test props->info->print should be
698
         * removed along with it.
699
         */
700
        if (props->info->print) {
701
            props->info->print(dev, props, buf, sizeof(buf));
702
            qdev_printf("%s-prop: %s = %s\n", prefix, props->name, buf);
703
        }
704
        props++;
705
    }
706
}
707

    
708
static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
709
{
710
    BusState *child;
711
    qdev_printf("dev: %s, id \"%s\"\n", dev->info->name,
712
                dev->id ? dev->id : "");
713
    indent += 2;
714
    if (dev->num_gpio_in) {
715
        qdev_printf("gpio-in %d\n", dev->num_gpio_in);
716
    }
717
    if (dev->num_gpio_out) {
718
        qdev_printf("gpio-out %d\n", dev->num_gpio_out);
719
    }
720
    qdev_print_props(mon, dev, dev->info->props, "dev", indent);
721
    qdev_print_props(mon, dev, dev->parent_bus->info->props, "bus", indent);
722
    if (dev->parent_bus->info->print_dev)
723
        dev->parent_bus->info->print_dev(mon, dev, indent);
724
    QLIST_FOREACH(child, &dev->child_bus, sibling) {
725
        qbus_print(mon, child, indent);
726
    }
727
}
728

    
729
static void qbus_print(Monitor *mon, BusState *bus, int indent)
730
{
731
    struct DeviceState *dev;
732

    
733
    qdev_printf("bus: %s\n", bus->name);
734
    indent += 2;
735
    qdev_printf("type %s\n", bus->info->name);
736
    QLIST_FOREACH(dev, &bus->children, sibling) {
737
        qdev_print(mon, dev, indent);
738
    }
739
}
740
#undef qdev_printf
741

    
742
void do_info_qtree(Monitor *mon)
743
{
744
    if (main_system_bus)
745
        qbus_print(mon, main_system_bus, 0);
746
}
747

    
748
void do_info_qdm(Monitor *mon)
749
{
750
    DeviceInfo *info;
751

    
752
    for (info = device_info_list; info != NULL; info = info->next) {
753
        qdev_print_devinfo(info);
754
    }
755
}
756

    
757
void do_device_add(Monitor *mon, const QDict *qdict)
758
{
759
    QemuOpts *opts;
760

    
761
    opts = qemu_opts_parse(&qemu_device_opts,
762
                           qdict_get_str(qdict, "config"), "driver");
763
    if (opts) {
764
        if (qdev_device_help(opts) || qdev_device_add(opts) == NULL) {
765
            qemu_opts_del(opts);
766
        }
767
    }
768
}
769

    
770
void do_device_del(Monitor *mon, const QDict *qdict)
771
{
772
    const char *id = qdict_get_str(qdict, "id");
773
    DeviceState *dev;
774

    
775
    dev = qdev_find_recursive(main_system_bus, id);
776
    if (NULL == dev) {
777
        error_report("Device '%s' not found", id);
778
        return;
779
    }
780
    qdev_unplug(dev);
781
}