]> git.proxmox.com Git - qemu.git/blame - hw/ide/qdev.c
Rearrange block headers
[qemu.git] / hw / ide / qdev.c
CommitLineData
da4d0419
GH
1/*
2 * ide bus support for qdev.
3 *
4 * Copyright (c) 2009 Gerd Hoffmann <kraxel@redhat.com>
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#include <hw/hw.h>
da4d0419 20#include "dma.h"
f597627f 21#include "qemu-error.h"
da4d0419 22#include <hw/ide/internal.h>
2446333c 23#include "blockdev.h"
da4d0419
GH
24
25/* --------------------------------- */
26
27static struct BusInfo ide_bus_info = {
28 .name = "IDE",
29 .size = sizeof(IDEBus),
30};
31
1f850f10 32void ide_bus_new(IDEBus *idebus, DeviceState *dev)
da4d0419 33{
1f850f10 34 qbus_create_inplace(&idebus->qbus, &ide_bus_info, dev, NULL);
da4d0419
GH
35}
36
37static int ide_qdev_init(DeviceState *qdev, DeviceInfo *base)
38{
39 IDEDevice *dev = DO_UPCAST(IDEDevice, qdev, qdev);
40 IDEDeviceInfo *info = DO_UPCAST(IDEDeviceInfo, qdev, base);
41 IDEBus *bus = DO_UPCAST(IDEBus, qbus, qdev->parent_bus);
42
f8b6cc00 43 if (!dev->conf.bs) {
f597627f 44 error_report("No drive specified");
da4d0419
GH
45 goto err;
46 }
47 if (dev->unit == -1) {
48 dev->unit = bus->master ? 1 : 0;
49 }
50 switch (dev->unit) {
51 case 0:
52 if (bus->master) {
f597627f 53 error_report("IDE unit %d is in use", dev->unit);
da4d0419
GH
54 goto err;
55 }
56 bus->master = dev;
57 break;
58 case 1:
59 if (bus->slave) {
f597627f 60 error_report("IDE unit %d is in use", dev->unit);
da4d0419
GH
61 goto err;
62 }
63 bus->slave = dev;
64 break;
65 default:
f597627f 66 error_report("Invalid IDE unit %d", dev->unit);
da4d0419
GH
67 goto err;
68 }
69 return info->init(dev);
70
71err:
72 return -1;
73}
74
75static void ide_qdev_register(IDEDeviceInfo *info)
76{
77 info->qdev.init = ide_qdev_init;
78 info->qdev.bus_info = &ide_bus_info;
79 qdev_register(&info->qdev);
80}
81
82IDEDevice *ide_create_drive(IDEBus *bus, int unit, DriveInfo *drive)
83{
84 DeviceState *dev;
85
86 dev = qdev_create(&bus->qbus, "ide-drive");
87 qdev_prop_set_uint32(dev, "unit", unit);
18846dee 88 qdev_prop_set_drive_nofail(dev, "drive", drive->bdrv);
fa12fbbe 89 qdev_init_nofail(dev);
da4d0419
GH
90 return DO_UPCAST(IDEDevice, qdev, dev);
91}
92
c0897e0c
MA
93void ide_get_bs(BlockDriverState *bs[], BusState *qbus)
94{
95 IDEBus *bus = DO_UPCAST(IDEBus, qbus, qbus);
96 bs[0] = bus->master ? bus->master->conf.bs : NULL;
97 bs[1] = bus->slave ? bus->slave->conf.bs : NULL;
98}
99
da4d0419
GH
100/* --------------------------------- */
101
102typedef struct IDEDrive {
103 IDEDevice dev;
104} IDEDrive;
105
106static int ide_drive_initfn(IDEDevice *dev)
107{
108 IDEBus *bus = DO_UPCAST(IDEBus, qbus, dev->qdev.parent_bus);
6ced55a5
MA
109 IDEState *s = bus->ifs + dev->unit;
110 const char *serial;
f8b6cc00 111 DriveInfo *dinfo;
6ced55a5
MA
112
113 serial = dev->serial;
114 if (!serial) {
115 /* try to fall back to value set with legacy -drive serial=... */
f8b6cc00
MA
116 dinfo = drive_get_by_blockdev(dev->conf.bs);
117 if (*dinfo->serial) {
118 serial = dinfo->serial;
119 }
6ced55a5
MA
120 }
121
c4d74df7
MA
122 if (ide_init_drive(s, dev->conf.bs, dev->version, serial) < 0) {
123 return -1;
124 }
6ced55a5 125
03432407
MA
126 if (!dev->version) {
127 dev->version = qemu_strdup(s->version);
128 }
6ced55a5
MA
129 if (!dev->serial) {
130 dev->serial = qemu_strdup(s->drive_serial_str);
131 }
da4d0419
GH
132 return 0;
133}
134
135static IDEDeviceInfo ide_drive_info = {
136 .qdev.name = "ide-drive",
137 .qdev.size = sizeof(IDEDrive),
138 .init = ide_drive_initfn,
139 .qdev.props = (Property[]) {
140 DEFINE_PROP_UINT32("unit", IDEDrive, dev.unit, -1),
428c149b 141 DEFINE_BLOCK_PROPERTIES(IDEDrive, dev.conf),
47c06340 142 DEFINE_PROP_STRING("ver", IDEDrive, dev.version),
6ced55a5 143 DEFINE_PROP_STRING("serial", IDEDrive, dev.serial),
da4d0419
GH
144 DEFINE_PROP_END_OF_LIST(),
145 }
146};
147
148static void ide_drive_register(void)
149{
150 ide_qdev_register(&ide_drive_info);
151}
152device_init(ide_drive_register);