Annotation of qemu/hw/ide/qdev.c, revision 1.1.1.4

1.1       root        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>
                     20: #include "dma.h"
1.1.1.3   root       21: #include "qemu-error.h"
1.1       root       22: #include <hw/ide/internal.h>
1.1.1.4 ! root       23: #include "blockdev.h"
        !            24: #include "sysemu.h"
1.1       root       25: 
                     26: /* --------------------------------- */
                     27: 
1.1.1.4 ! root       28: static char *idebus_get_fw_dev_path(DeviceState *dev);
        !            29: 
1.1       root       30: static struct BusInfo ide_bus_info = {
                     31:     .name  = "IDE",
                     32:     .size  = sizeof(IDEBus),
1.1.1.4 ! root       33:     .get_fw_dev_path = idebus_get_fw_dev_path,
1.1       root       34: };
                     35: 
1.1.1.4 ! root       36: void ide_bus_new(IDEBus *idebus, DeviceState *dev, int bus_id)
1.1       root       37: {
                     38:     qbus_create_inplace(&idebus->qbus, &ide_bus_info, dev, NULL);
1.1.1.4 ! root       39:     idebus->bus_id = bus_id;
        !            40: }
        !            41: 
        !            42: static char *idebus_get_fw_dev_path(DeviceState *dev)
        !            43: {
        !            44:     char path[30];
        !            45: 
        !            46:     snprintf(path, sizeof(path), "%s@%d", qdev_fw_name(dev),
        !            47:              ((IDEBus*)dev->parent_bus)->bus_id);
        !            48: 
        !            49:     return strdup(path);
1.1       root       50: }
                     51: 
                     52: static int ide_qdev_init(DeviceState *qdev, DeviceInfo *base)
                     53: {
                     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);
                     57: 
1.1.1.3   root       58:     if (!dev->conf.bs) {
                     59:         error_report("No drive specified");
1.1       root       60:         goto err;
                     61:     }
                     62:     if (dev->unit == -1) {
                     63:         dev->unit = bus->master ? 1 : 0;
                     64:     }
                     65:     switch (dev->unit) {
                     66:     case 0:
                     67:         if (bus->master) {
1.1.1.3   root       68:             error_report("IDE unit %d is in use", dev->unit);
1.1       root       69:             goto err;
                     70:         }
                     71:         bus->master = dev;
                     72:         break;
                     73:     case 1:
                     74:         if (bus->slave) {
1.1.1.3   root       75:             error_report("IDE unit %d is in use", dev->unit);
1.1       root       76:             goto err;
                     77:         }
                     78:         bus->slave = dev;
                     79:         break;
                     80:     default:
1.1.1.3   root       81:         error_report("Invalid IDE unit %d", dev->unit);
1.1       root       82:         goto err;
                     83:     }
                     84:     return info->init(dev);
                     85: 
                     86: err:
                     87:     return -1;
                     88: }
                     89: 
                     90: static void ide_qdev_register(IDEDeviceInfo *info)
                     91: {
                     92:     info->qdev.init = ide_qdev_init;
                     93:     info->qdev.bus_info = &ide_bus_info;
                     94:     qdev_register(&info->qdev);
                     95: }
                     96: 
                     97: IDEDevice *ide_create_drive(IDEBus *bus, int unit, DriveInfo *drive)
                     98: {
                     99:     DeviceState *dev;
                    100: 
                    101:     dev = qdev_create(&bus->qbus, "ide-drive");
                    102:     qdev_prop_set_uint32(dev, "unit", unit);
1.1.1.3   root      103:     qdev_prop_set_drive_nofail(dev, "drive", drive->bdrv);
                    104:     qdev_init_nofail(dev);
1.1       root      105:     return DO_UPCAST(IDEDevice, qdev, dev);
                    106: }
                    107: 
1.1.1.3   root      108: void ide_get_bs(BlockDriverState *bs[], BusState *qbus)
                    109: {
                    110:     IDEBus *bus = DO_UPCAST(IDEBus, qbus, qbus);
                    111:     bs[0] = bus->master ? bus->master->conf.bs : NULL;
                    112:     bs[1] = bus->slave  ? bus->slave->conf.bs  : NULL;
                    113: }
                    114: 
1.1       root      115: /* --------------------------------- */
                    116: 
                    117: typedef struct IDEDrive {
                    118:     IDEDevice dev;
                    119: } IDEDrive;
                    120: 
                    121: static int ide_drive_initfn(IDEDevice *dev)
                    122: {
                    123:     IDEBus *bus = DO_UPCAST(IDEBus, qbus, dev->qdev.parent_bus);
1.1.1.3   root      124:     IDEState *s = bus->ifs + dev->unit;
                    125:     const char *serial;
                    126:     DriveInfo *dinfo;
                    127: 
                    128:     serial = dev->serial;
                    129:     if (!serial) {
                    130:         /* try to fall back to value set with legacy -drive serial=... */
                    131:         dinfo = drive_get_by_blockdev(dev->conf.bs);
                    132:         if (*dinfo->serial) {
                    133:             serial = dinfo->serial;
                    134:         }
                    135:     }
                    136: 
                    137:     if (ide_init_drive(s, dev->conf.bs, dev->version, serial) < 0) {
                    138:         return -1;
                    139:     }
                    140: 
                    141:     if (!dev->version) {
                    142:         dev->version = qemu_strdup(s->version);
                    143:     }
                    144:     if (!dev->serial) {
                    145:         dev->serial = qemu_strdup(s->drive_serial_str);
                    146:     }
1.1.1.4 ! root      147: 
        !           148:     add_boot_device_path(dev->conf.bootindex, &dev->qdev,
        !           149:                          dev->unit ? "/disk@1" : "/disk@0");
        !           150: 
1.1       root      151:     return 0;
                    152: }
                    153: 
                    154: static IDEDeviceInfo ide_drive_info = {
                    155:     .qdev.name  = "ide-drive",
1.1.1.4 ! root      156:     .qdev.fw_name  = "drive",
1.1       root      157:     .qdev.size  = sizeof(IDEDrive),
                    158:     .init       = ide_drive_initfn,
                    159:     .qdev.props = (Property[]) {
                    160:         DEFINE_PROP_UINT32("unit", IDEDrive, dev.unit, -1),
1.1.1.3   root      161:         DEFINE_BLOCK_PROPERTIES(IDEDrive, dev.conf),
1.1.1.2   root      162:         DEFINE_PROP_STRING("ver",  IDEDrive, dev.version),
1.1.1.3   root      163:         DEFINE_PROP_STRING("serial",  IDEDrive, dev.serial),
1.1       root      164:         DEFINE_PROP_END_OF_LIST(),
                    165:     }
                    166: };
                    167: 
                    168: static void ide_drive_register(void)
                    169: {
                    170:     ide_qdev_register(&ide_drive_info);
                    171: }
                    172: device_init(ide_drive_register);

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.