]> git.proxmox.com Git - qemu.git/blobdiff - hw/eeprom93xx.c
Merge remote-tracking branch 'spice/spice.v39' into staging
[qemu.git] / hw / eeprom93xx.c
index cf778b74102a9cd9e4b943f0c0362b461859945a..7b21f98e22d4334593813f74549e94e5df4c936c 100644 (file)
@@ -14,8 +14,7 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
 /* Emulation for serial EEPROMs:
@@ -76,7 +75,7 @@ struct _eeprom_t {
     uint8_t  tick;
     uint8_t  address;
     uint8_t  command;
-    uint8_t  writeable;
+    uint8_t  writable;
 
     uint8_t eecs;
     uint8_t eesk;
@@ -90,66 +89,63 @@ struct _eeprom_t {
 
 /* Code for saving and restoring of EEPROM state. */
 
-static void eeprom_save(QEMUFile *f, void *opaque)
+/* Restore an uint16_t from an uint8_t
+   This is a Big hack, but it is how the old state did it.
+ */
+
+static int get_uint16_from_uint8(QEMUFile *f, void *pv, size_t size)
 {
-    /* Save EEPROM data. */
-    unsigned address;
-    eeprom_t *eeprom = (eeprom_t *)opaque;
+    uint16_t *v = pv;
+    *v = qemu_get_ubyte(f);
+    return 0;
+}
 
-    qemu_put_byte(f, eeprom->tick);
-    qemu_put_byte(f, eeprom->address);
-    qemu_put_byte(f, eeprom->command);
-    qemu_put_byte(f, eeprom->writeable);
+static void put_unused(QEMUFile *f, void *pv, size_t size)
+{
+    fprintf(stderr, "uint16_from_uint8 is used only for backwards compatibility.\n");
+    fprintf(stderr, "Never should be used to write a new state.\n");
+    exit(0);
+}
 
-    qemu_put_byte(f, eeprom->eecs);
-    qemu_put_byte(f, eeprom->eesk);
-    qemu_put_byte(f, eeprom->eedo);
+static const VMStateInfo vmstate_hack_uint16_from_uint8 = {
+    .name = "uint16_from_uint8",
+    .get  = get_uint16_from_uint8,
+    .put  = put_unused,
+};
 
-    qemu_put_byte(f, eeprom->addrbits);
-    qemu_put_be16(f, eeprom->size);
-    qemu_put_be16(f, eeprom->data);
-    for (address = 0; address < eeprom->size; address++) {
-        qemu_put_be16(f, eeprom->contents[address]);
-    }
-}
+#define VMSTATE_UINT16_HACK_TEST(_f, _s, _t)                           \
+    VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint16_from_uint8, uint16_t)
 
-static int eeprom_load(QEMUFile *f, void *opaque, int version_id)
+static bool is_old_eeprom_version(void *opaque, int version_id)
 {
-    /* Load EEPROM data from saved data if version and EEPROM size
-       of data and current EEPROM are identical. */
-    eeprom_t *eeprom = (eeprom_t *)opaque;
-    int result = -EINVAL;
-    if (version_id >= OLD_EEPROM_VERSION) {
-        unsigned address;
-        int size = eeprom->size;
-
-        eeprom->tick = qemu_get_byte(f);
-        eeprom->address = qemu_get_byte(f);
-        eeprom->command = qemu_get_byte(f);
-        eeprom->writeable = qemu_get_byte(f);
+    return version_id == OLD_EEPROM_VERSION;
+}
 
-        eeprom->eecs = qemu_get_byte(f);
-        eeprom->eesk = qemu_get_byte(f);
-        eeprom->eedo = qemu_get_byte(f);
+static const VMStateDescription vmstate_eeprom = {
+    .name = "eeprom",
+    .version_id = EEPROM_VERSION,
+    .minimum_version_id = OLD_EEPROM_VERSION,
+    .minimum_version_id_old = OLD_EEPROM_VERSION,
+    .fields      = (VMStateField []) {
+        VMSTATE_UINT8(tick, eeprom_t),
+        VMSTATE_UINT8(address, eeprom_t),
+        VMSTATE_UINT8(command, eeprom_t),
+        VMSTATE_UINT8(writable, eeprom_t),
 
-        eeprom->addrbits = qemu_get_byte(f);
-        if (version_id == OLD_EEPROM_VERSION) {
-            eeprom->size = qemu_get_byte(f);
-            qemu_get_byte(f);
-        } else {
-            eeprom->size = qemu_get_be16(f);
-        }
+        VMSTATE_UINT8(eecs, eeprom_t),
+        VMSTATE_UINT8(eesk, eeprom_t),
+        VMSTATE_UINT8(eedo, eeprom_t),
 
-        if (eeprom->size == size) {
-            eeprom->data = qemu_get_be16(f);
-            for (address = 0; address < eeprom->size; address++) {
-                eeprom->contents[address] = qemu_get_be16(f);
-            }
-            result = 0;
-        }
+        VMSTATE_UINT8(addrbits, eeprom_t),
+        VMSTATE_UINT16_HACK_TEST(size, eeprom_t, is_old_eeprom_version),
+        VMSTATE_UNUSED_TEST(is_old_eeprom_version, 1),
+        VMSTATE_UINT16_EQUAL_V(size, eeprom_t, EEPROM_VERSION),
+        VMSTATE_UINT16(data, eeprom_t),
+        VMSTATE_VARRAY_UINT16_UNSAFE(contents, eeprom_t, size, 0,
+                                     vmstate_info_uint16, uint16_t),
+        VMSTATE_END_OF_LIST()
     }
-    return result;
-}
+};
 
 void eeprom93xx_write(eeprom_t *eeprom, int eecs, int eesk, int eedi)
 {
@@ -169,7 +165,7 @@ void eeprom93xx_write(eeprom_t *eeprom, int eecs, int eesk, int eedi)
         address = 0x0;
     } else if (eeprom->eecs && ! eecs) {
         /* End chip select cycle. This triggers write / erase. */
-        if (eeprom->writeable) {
+        if (eeprom->writable) {
             uint8_t subcommand = address >> (eeprom->addrbits - 2);
             if (command == 0 && subcommand == 2) {
                 /* Erase all. */
@@ -236,7 +232,7 @@ void eeprom93xx_write(eeprom_t *eeprom, int eecs, int eesk, int eedi)
                     switch (address >> (eeprom->addrbits - 2)) {
                         case 0:
                             logout("write disable command\n");
-                            eeprom->writeable = 0;
+                            eeprom->writable = 0;
                             break;
                         case 1:
                             logout("write all command\n");
@@ -246,7 +242,7 @@ void eeprom93xx_write(eeprom_t *eeprom, int eecs, int eesk, int eedi)
                             break;
                         case 3:
                             logout("write enable command\n");
-                            eeprom->writeable = 1;
+                            eeprom->writable = 1;
                             break;
                     }
                 } else {
@@ -293,7 +289,7 @@ void eeprom93xx_reset(eeprom_t *eeprom)
 }
 #endif
 
-eeprom_t *eeprom93xx_new(uint16_t nwords)
+eeprom_t *eeprom93xx_new(DeviceState *dev, uint16_t nwords)
 {
     /* Add a new EEPROM (with 16, 64 or 256 words). */
     eeprom_t *eeprom;
@@ -320,16 +316,15 @@ eeprom_t *eeprom93xx_new(uint16_t nwords)
     /* Output DO is tristate, read results in 1. */
     eeprom->eedo = 1;
     logout("eeprom = 0x%p, nwords = %u\n", eeprom, nwords);
-    register_savevm("eeprom", EEPROM_INSTANCE, EEPROM_VERSION,
-                    eeprom_save, eeprom_load, eeprom);
+    vmstate_register(dev, 0, &vmstate_eeprom, eeprom);
     return eeprom;
 }
 
-void eeprom93xx_free(eeprom_t *eeprom)
+void eeprom93xx_free(DeviceState *dev, eeprom_t *eeprom)
 {
     /* Destroy EEPROM. */
     logout("eeprom = 0x%p\n", eeprom);
-    unregister_savevm("eeprom", eeprom);
+    vmstate_unregister(dev, &vmstate_eeprom, eeprom);
     qemu_free(eeprom);
 }