ide/qdev: add ide bus.
[qemu.git] / hw / ide / qdev.c
blobd467e3ad226aecf062c2a4fb8326e0002cc36794
1 /*
2 * ide bus support for qdev.
4 * Copyright (c) 2009 Gerd Hoffmann <kraxel@redhat.com>
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.
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.
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/>.
19 #include <hw/hw.h>
20 #include "sysemu.h"
21 #include "dma.h"
23 #include <hw/ide/internal.h>
25 /* --------------------------------- */
27 static struct BusInfo ide_bus_info = {
28 .name = "IDE",
29 .size = sizeof(IDEBus),
32 IDEBus *ide_bus_new(DeviceState *dev)
34 IDEBus *idebus;
36 idebus = FROM_QBUS(IDEBus, qbus_create(&ide_bus_info, dev, NULL));
37 return idebus;
40 static int ide_qdev_init(DeviceState *qdev, DeviceInfo *base)
42 IDEDevice *dev = DO_UPCAST(IDEDevice, qdev, qdev);
43 IDEDeviceInfo *info = DO_UPCAST(IDEDeviceInfo, qdev, base);
44 IDEBus *bus = DO_UPCAST(IDEBus, qbus, qdev->parent_bus);
46 if (!dev->dinfo) {
47 fprintf(stderr, "%s: no drive specified\n", qdev->info->name);
48 goto err;
50 if (dev->unit == -1) {
51 dev->unit = bus->master ? 1 : 0;
53 switch (dev->unit) {
54 case 0:
55 if (bus->master) {
56 fprintf(stderr, "ide: tried to assign master twice\n");
57 goto err;
59 bus->master = dev;
60 break;
61 case 1:
62 if (bus->slave) {
63 fprintf(stderr, "ide: tried to assign slave twice\n");
64 goto err;
66 bus->slave = dev;
67 break;
68 default:
69 goto err;
71 return info->init(dev);
73 err:
74 return -1;
77 static void ide_qdev_register(IDEDeviceInfo *info)
79 info->qdev.init = ide_qdev_init;
80 info->qdev.bus_info = &ide_bus_info;
81 qdev_register(&info->qdev);
84 IDEDevice *ide_create_drive(IDEBus *bus, int unit, DriveInfo *drive)
86 DeviceState *dev;
88 dev = qdev_create(&bus->qbus, "ide-drive");
89 qdev_prop_set_uint32(dev, "unit", unit);
90 qdev_prop_set_drive(dev, "drive", drive);
91 if (qdev_init(dev) != 0)
92 return NULL;
93 return DO_UPCAST(IDEDevice, qdev, dev);
96 /* --------------------------------- */
98 typedef struct IDEDrive {
99 IDEDevice dev;
100 } IDEDrive;
102 static int ide_drive_initfn(IDEDevice *dev)
104 IDEBus *bus = DO_UPCAST(IDEBus, qbus, dev->qdev.parent_bus);
105 ide_init_drive(bus->ifs + dev->unit, dev->dinfo);
106 return 0;
109 static IDEDeviceInfo ide_drive_info = {
110 .qdev.name = "ide-drive",
111 .qdev.size = sizeof(IDEDrive),
112 .init = ide_drive_initfn,
113 .qdev.props = (Property[]) {
114 DEFINE_PROP_UINT32("unit", IDEDrive, dev.unit, -1),
115 DEFINE_PROP_DRIVE("drive", IDEDrive, dev.dinfo),
116 DEFINE_PROP_END_OF_LIST(),
120 static void ide_drive_register(void)
122 ide_qdev_register(&ide_drive_info);
124 device_init(ide_drive_register);