Revision 46a5a89d hw/virtio/virtio-rng.c

b/hw/virtio/virtio-rng.c
130 130
                   qemu_get_clock_ms(vm_clock) + s->conf.period_ms);
131 131
}
132 132

  
133
static VirtIODevice *virtio_rng_common_init(DeviceState *dev,
134
                                            VirtIORNGConf *conf,
135
                                            VirtIORNG **pvrng)
133
static int virtio_rng_device_init(VirtIODevice *vdev)
136 134
{
137
    VirtIORNG *vrng = *pvrng;
138
    VirtIODevice *vdev;
135
    DeviceState *qdev = DEVICE(vdev);
136
    VirtIORNG *vrng = VIRTIO_RNG(vdev);
139 137
    Error *local_err = NULL;
140 138

  
141
    /*
142
     * We have two cases here: the old virtio-rng-x device, and the
143
     * refactored virtio-rng.
144
     * This will disappear later in the serie.
145
     */
146
    if (vrng == NULL) {
147
        vdev = virtio_common_init("virtio-rng", VIRTIO_ID_RNG, 0,
148
                                  sizeof(VirtIORNG));
149
        vrng = DO_UPCAST(VirtIORNG, vdev, vdev);
150
    } else {
151
        vdev = VIRTIO_DEVICE(vrng);
152
        virtio_init(vdev, "virtio-rng", VIRTIO_ID_RNG, 0);
139
    if (vrng->conf.rng == NULL) {
140
        vrng->conf.default_backend = RNG_RANDOM(object_new(TYPE_RNG_RANDOM));
141

  
142
        object_property_add_child(OBJECT(qdev),
143
                                  "default-backend",
144
                                  OBJECT(vrng->conf.default_backend),
145
                                  NULL);
146

  
147
        object_property_set_link(OBJECT(qdev),
148
                                 OBJECT(vrng->conf.default_backend),
149
                                 "rng", NULL);
153 150
    }
154 151

  
155
    vrng->rng = conf->rng;
152
    virtio_init(vdev, "virtio-rng", VIRTIO_ID_RNG, 0);
153

  
154
    vrng->rng = vrng->conf.rng;
156 155
    if (vrng->rng == NULL) {
157 156
        qerror_report(QERR_INVALID_PARAMETER_VALUE, "rng", "a valid object");
158
        return NULL;
157
        return -1;
159 158
    }
160 159

  
161 160
    rng_backend_open(vrng->rng, &local_err);
162 161
    if (local_err) {
163 162
        qerror_report_err(local_err);
164 163
        error_free(local_err);
165
        return NULL;
164
        return -1;
166 165
    }
167 166

  
168 167
    vrng->vq = virtio_add_queue(vdev, 8, handle_input);
169 168

  
170 169
    vrng->vdev.get_features = get_features;
171 170

  
172
    vrng->qdev = dev;
173
    memcpy(&(vrng->conf), conf, sizeof(struct VirtIORNGConf));
171
    vrng->qdev = qdev;
174 172

  
175 173
    assert(vrng->conf.max_bytes <= INT64_MAX);
176 174
    vrng->quota_remaining = vrng->conf.max_bytes;
......
181 179
    qemu_mod_timer(vrng->rate_limit_timer,
182 180
                   qemu_get_clock_ms(vm_clock) + vrng->conf.period_ms);
183 181

  
184
    register_savevm(dev, "virtio-rng", -1, 1, virtio_rng_save,
182
    register_savevm(qdev, "virtio-rng", -1, 1, virtio_rng_save,
185 183
                    virtio_rng_load, vrng);
186 184

  
187
    return vdev;
188
}
189

  
190
/*
191
 * This two functions will be removed later in the serie.
192
 */
193
VirtIODevice *virtio_rng_init(DeviceState *dev, VirtIORNGConf *conf)
194
{
195
    VirtIORNG *vdev = NULL;
196
    return virtio_rng_common_init(dev, conf, &vdev);
197
}
198

  
199
void virtio_rng_exit(VirtIODevice *vdev)
200
{
201
    VirtIORNG *vrng = DO_UPCAST(VirtIORNG, vdev, vdev);
202

  
203
    qemu_del_timer(vrng->rate_limit_timer);
204
    qemu_free_timer(vrng->rate_limit_timer);
205
    unregister_savevm(vrng->qdev, "virtio-rng", vrng);
206
    virtio_cleanup(vdev);
207
}
208

  
209
static int virtio_rng_device_init(VirtIODevice *vdev)
210
{
211
    DeviceState *qdev = DEVICE(vdev);
212
    VirtIORNG *vrng = VIRTIO_RNG(vdev);
213

  
214
    if (vrng->conf.rng == NULL) {
215
        vrng->conf.default_backend = RNG_RANDOM(object_new(TYPE_RNG_RANDOM));
216

  
217
        object_property_add_child(OBJECT(qdev),
218
                                  "default-backend",
219
                                  OBJECT(vrng->conf.default_backend),
220
                                  NULL);
221

  
222
        object_property_set_link(OBJECT(qdev),
223
                                 OBJECT(vrng->conf.default_backend),
224
                                 "rng", NULL);
225
    }
226

  
227
    if (virtio_rng_common_init(qdev, &(vrng->conf), &vrng) == NULL) {
228
        return -1;
229
    }
230 185
    return 0;
231 186
}
232 187

  

Also available in: Unified diff