]>
Commit | Line | Data |
---|---|---|
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 GH |
20 | #include "dma.h" |
21 | ||
22 | #include <hw/ide/internal.h> | |
23 | ||
24 | /* --------------------------------- */ | |
25 | ||
26 | static struct BusInfo ide_bus_info = { | |
27 | .name = "IDE", | |
28 | .size = sizeof(IDEBus), | |
29 | }; | |
30 | ||
1f850f10 | 31 | void ide_bus_new(IDEBus *idebus, DeviceState *dev) |
da4d0419 | 32 | { |
1f850f10 | 33 | qbus_create_inplace(&idebus->qbus, &ide_bus_info, dev, NULL); |
da4d0419 GH |
34 | } |
35 | ||
36 | static int ide_qdev_init(DeviceState *qdev, DeviceInfo *base) | |
37 | { | |
38 | IDEDevice *dev = DO_UPCAST(IDEDevice, qdev, qdev); | |
39 | IDEDeviceInfo *info = DO_UPCAST(IDEDeviceInfo, qdev, base); | |
40 | IDEBus *bus = DO_UPCAST(IDEBus, qbus, qdev->parent_bus); | |
41 | ||
428c149b | 42 | if (!dev->conf.dinfo) { |
da4d0419 GH |
43 | fprintf(stderr, "%s: no drive specified\n", qdev->info->name); |
44 | goto err; | |
45 | } | |
46 | if (dev->unit == -1) { | |
47 | dev->unit = bus->master ? 1 : 0; | |
48 | } | |
49 | switch (dev->unit) { | |
50 | case 0: | |
51 | if (bus->master) { | |
52 | fprintf(stderr, "ide: tried to assign master twice\n"); | |
53 | goto err; | |
54 | } | |
55 | bus->master = dev; | |
56 | break; | |
57 | case 1: | |
58 | if (bus->slave) { | |
59 | fprintf(stderr, "ide: tried to assign slave twice\n"); | |
60 | goto err; | |
61 | } | |
62 | bus->slave = dev; | |
63 | break; | |
64 | default: | |
65 | goto err; | |
66 | } | |
67 | return info->init(dev); | |
68 | ||
69 | err: | |
70 | return -1; | |
71 | } | |
72 | ||
73 | static void ide_qdev_register(IDEDeviceInfo *info) | |
74 | { | |
75 | info->qdev.init = ide_qdev_init; | |
76 | info->qdev.bus_info = &ide_bus_info; | |
77 | qdev_register(&info->qdev); | |
78 | } | |
79 | ||
80 | IDEDevice *ide_create_drive(IDEBus *bus, int unit, DriveInfo *drive) | |
81 | { | |
82 | DeviceState *dev; | |
83 | ||
84 | dev = qdev_create(&bus->qbus, "ide-drive"); | |
85 | qdev_prop_set_uint32(dev, "unit", unit); | |
86 | qdev_prop_set_drive(dev, "drive", drive); | |
fa12fbbe | 87 | qdev_init_nofail(dev); |
da4d0419 GH |
88 | return DO_UPCAST(IDEDevice, qdev, dev); |
89 | } | |
90 | ||
91 | /* --------------------------------- */ | |
92 | ||
93 | typedef struct IDEDrive { | |
94 | IDEDevice dev; | |
95 | } IDEDrive; | |
96 | ||
97 | static int ide_drive_initfn(IDEDevice *dev) | |
98 | { | |
99 | IDEBus *bus = DO_UPCAST(IDEBus, qbus, dev->qdev.parent_bus); | |
6ced55a5 MA |
100 | IDEState *s = bus->ifs + dev->unit; |
101 | const char *serial; | |
102 | ||
103 | serial = dev->serial; | |
104 | if (!serial) { | |
105 | /* try to fall back to value set with legacy -drive serial=... */ | |
106 | serial = dev->conf.dinfo->serial; | |
107 | } | |
108 | ||
109 | ide_init_drive(s, dev->conf.dinfo, dev->version, serial); | |
110 | ||
03432407 MA |
111 | if (!dev->version) { |
112 | dev->version = qemu_strdup(s->version); | |
113 | } | |
6ced55a5 MA |
114 | if (!dev->serial) { |
115 | dev->serial = qemu_strdup(s->drive_serial_str); | |
116 | } | |
da4d0419 GH |
117 | return 0; |
118 | } | |
119 | ||
120 | static IDEDeviceInfo ide_drive_info = { | |
121 | .qdev.name = "ide-drive", | |
122 | .qdev.size = sizeof(IDEDrive), | |
123 | .init = ide_drive_initfn, | |
124 | .qdev.props = (Property[]) { | |
125 | DEFINE_PROP_UINT32("unit", IDEDrive, dev.unit, -1), | |
428c149b | 126 | DEFINE_BLOCK_PROPERTIES(IDEDrive, dev.conf), |
47c06340 | 127 | DEFINE_PROP_STRING("ver", IDEDrive, dev.version), |
6ced55a5 | 128 | DEFINE_PROP_STRING("serial", IDEDrive, dev.serial), |
da4d0419 GH |
129 | DEFINE_PROP_END_OF_LIST(), |
130 | } | |
131 | }; | |
132 | ||
133 | static void ide_drive_register(void) | |
134 | { | |
135 | ide_qdev_register(&ide_drive_info); | |
136 | } | |
137 | device_init(ide_drive_register); |