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