ide: Split qdev "ide-drive" into "ide-hd" and "ide-cd"
[qemu.git] / hw / ide / qdev.c
blob3bca726972589809467b381f36d26f4c4eecccd4
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 "dma.h"
21 #include "qemu-error.h"
22 #include <hw/ide/internal.h>
23 #include "blockdev.h"
24 #include "sysemu.h"
26 /* --------------------------------- */
28 static char *idebus_get_fw_dev_path(DeviceState *dev);
30 static struct BusInfo ide_bus_info = {
31 .name = "IDE",
32 .size = sizeof(IDEBus),
33 .get_fw_dev_path = idebus_get_fw_dev_path,
36 void ide_bus_new(IDEBus *idebus, DeviceState *dev, int bus_id)
38 qbus_create_inplace(&idebus->qbus, &ide_bus_info, dev, NULL);
39 idebus->bus_id = bus_id;
42 static char *idebus_get_fw_dev_path(DeviceState *dev)
44 char path[30];
46 snprintf(path, sizeof(path), "%s@%d", qdev_fw_name(dev),
47 ((IDEBus*)dev->parent_bus)->bus_id);
49 return strdup(path);
52 static int ide_qdev_init(DeviceState *qdev, DeviceInfo *base)
54 IDEDevice *dev = DO_UPCAST(IDEDevice, qdev, qdev);
55 IDEDeviceInfo *info = DO_UPCAST(IDEDeviceInfo, qdev, base);
56 IDEBus *bus = DO_UPCAST(IDEBus, qbus, qdev->parent_bus);
58 if (!dev->conf.bs) {
59 error_report("No drive specified");
60 goto err;
62 if (dev->unit == -1) {
63 dev->unit = bus->master ? 1 : 0;
65 switch (dev->unit) {
66 case 0:
67 if (bus->master) {
68 error_report("IDE unit %d is in use", dev->unit);
69 goto err;
71 bus->master = dev;
72 break;
73 case 1:
74 if (bus->slave) {
75 error_report("IDE unit %d is in use", dev->unit);
76 goto err;
78 bus->slave = dev;
79 break;
80 default:
81 error_report("Invalid IDE unit %d", dev->unit);
82 goto err;
84 return info->init(dev);
86 err:
87 return -1;
90 static void ide_qdev_register(IDEDeviceInfo *info)
92 info->qdev.init = ide_qdev_init;
93 info->qdev.bus_info = &ide_bus_info;
94 qdev_register(&info->qdev);
97 IDEDevice *ide_create_drive(IDEBus *bus, int unit, DriveInfo *drive)
99 DeviceState *dev;
101 dev = qdev_create(&bus->qbus,
102 bdrv_get_type_hint(drive->bdrv) == BDRV_TYPE_CDROM
103 ? "ide-cd" : "ide-hd");
104 qdev_prop_set_uint32(dev, "unit", unit);
105 qdev_prop_set_drive_nofail(dev, "drive", drive->bdrv);
106 qdev_init_nofail(dev);
107 return DO_UPCAST(IDEDevice, qdev, dev);
110 void ide_get_bs(BlockDriverState *bs[], BusState *qbus)
112 IDEBus *bus = DO_UPCAST(IDEBus, qbus, qbus);
113 bs[0] = bus->master ? bus->master->conf.bs : NULL;
114 bs[1] = bus->slave ? bus->slave->conf.bs : NULL;
117 /* --------------------------------- */
119 typedef struct IDEDrive {
120 IDEDevice dev;
121 } IDEDrive;
123 static int ide_dev_initfn(IDEDevice *dev, IDEDriveKind kind)
125 IDEBus *bus = DO_UPCAST(IDEBus, qbus, dev->qdev.parent_bus);
126 IDEState *s = bus->ifs + dev->unit;
127 const char *serial;
128 DriveInfo *dinfo;
130 serial = dev->serial;
131 if (!serial) {
132 /* try to fall back to value set with legacy -drive serial=... */
133 dinfo = drive_get_by_blockdev(dev->conf.bs);
134 if (*dinfo->serial) {
135 serial = dinfo->serial;
139 if (ide_init_drive(s, dev->conf.bs, kind, dev->version, serial) < 0) {
140 return -1;
143 if (!dev->version) {
144 dev->version = qemu_strdup(s->version);
146 if (!dev->serial) {
147 dev->serial = qemu_strdup(s->drive_serial_str);
150 add_boot_device_path(dev->conf.bootindex, &dev->qdev,
151 dev->unit ? "/disk@1" : "/disk@0");
153 return 0;
156 static int ide_hd_initfn(IDEDevice *dev)
158 return ide_dev_initfn(dev, IDE_HD);
161 static int ide_cd_initfn(IDEDevice *dev)
163 return ide_dev_initfn(dev, IDE_CD);
166 static int ide_drive_initfn(IDEDevice *dev)
168 return ide_dev_initfn(dev,
169 bdrv_get_type_hint(dev->conf.bs) == BDRV_TYPE_CDROM
170 ? IDE_CD : IDE_HD);
173 #define DEFINE_IDE_DEV_PROPERTIES() \
174 DEFINE_PROP_UINT32("unit", IDEDrive, dev.unit, -1), \
175 DEFINE_BLOCK_PROPERTIES(IDEDrive, dev.conf), \
176 DEFINE_PROP_STRING("ver", IDEDrive, dev.version), \
177 DEFINE_PROP_STRING("serial", IDEDrive, dev.serial)
179 static IDEDeviceInfo ide_dev_info[] = {
181 .qdev.name = "ide-hd",
182 .qdev.fw_name = "drive",
183 .qdev.desc = "virtual IDE disk",
184 .qdev.size = sizeof(IDEDrive),
185 .init = ide_hd_initfn,
186 .qdev.props = (Property[]) {
187 DEFINE_IDE_DEV_PROPERTIES(),
188 DEFINE_PROP_END_OF_LIST(),
191 .qdev.name = "ide-cd",
192 .qdev.fw_name = "drive",
193 .qdev.desc = "virtual IDE CD-ROM",
194 .qdev.size = sizeof(IDEDrive),
195 .init = ide_cd_initfn,
196 .qdev.props = (Property[]) {
197 DEFINE_IDE_DEV_PROPERTIES(),
198 DEFINE_PROP_END_OF_LIST(),
201 .qdev.name = "ide-drive", /* legacy -device ide-drive */
202 .qdev.fw_name = "drive",
203 .qdev.desc = "virtual IDE disk or CD-ROM (legacy)",
204 .qdev.size = sizeof(IDEDrive),
205 .init = ide_drive_initfn,
206 .qdev.props = (Property[]) {
207 DEFINE_IDE_DEV_PROPERTIES(),
208 DEFINE_PROP_END_OF_LIST(),
213 static void ide_dev_register(void)
215 int i;
217 for (i = 0; i < ARRAY_SIZE(ide_dev_info); i++) {
218 ide_qdev_register(&ide_dev_info[i]);
221 device_init(ide_dev_register);